using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;
using UnityEngine.UI;
using static ResourceManager;
using static EntityManager;
using UnityEngine.EventSystems;

public class MainPanel : MonoBehaviour,IDragHandler
{
    public float scale;
    public CameraController cc;
    public GameObject[] buildings;
    private int type;
    public Button testBtn;
    public Dropdown dropdown;
    public Button mapMask;

    void Awake()
    {
        var mgr = World.DefaultGameObjectInjectionWorld.EntityManager;
        testBtn.onClick.AddListener(() => {
            var entity = mgr.CreateEntity();
            mgr.AddComponentData(entity, new BuildingComponent()
            {
                type = type,
                Id = (int)Time.time,
                cycle = 1,
                output = type
            });
            type++;
        });

        dropdown.ClearOptions();
        var list = new List<Dropdown.OptionData>();
        foreach (var go in buildings)
        {
            list.Add(new Dropdown.OptionData()
            {
                text = go.name
            });
        }
        dropdown.AddOptions(list);
        dropdown.onValueChanged.AddListener((value) =>
        {
            var pos = GetSystem<RaycastSystem>().GetSelectPos();
            var findEntity = GetEntityByPos(pos);
            if (findEntity != Entity.Null)
            {
                Manager.AddComponentData(findEntity, new DestroyComponent());
            }
            RaycastSystem.id = buildings[value].GetInstanceID();
            LoadObjectToEntity(buildings[value].GetInstanceID(), pos, scale);
        });

        mapMask.onClick.AddListener(RaycastSystem.OnClick);
    }

    public void OnDrag(PointerEventData eventData)
    {
        cc.OnDrag(eventData.delta);
    }
}
